{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 决策树 Decision Trees\n",
    "\n",
    "> 一种用来 classification 和 regression 的无参监督学习方法。其目的是创建一种模型从数据特征中学习简单的决策规则来预测一个目标变量的值\n",
    "\n",
    "逻辑回归是将所有特征变换为概率后，通过大于某一概率阈值的划分为一类，小于某一概率阈值的为另一类；\n",
    "而决策树是对每一个特征做一个划分。\n",
    "另外逻辑回归只能找到线性分割（输入特征x与logit之间是线性的，除非对x进行多维映射），而决策树可以找到非线性分割。\n",
    "\n",
    "[决策树](https://blog.csdn.net/qq_20412595/article/details/82048795)\n",
    "[sklearn文档-决策树](https://sklearn.apachecn.org/docs/master/11.html)\n",
    "\n",
    "一、什么是决策树\n",
    "\n",
    "  所谓决策树，就是一个类似于流程图的树形结构，树内部的每一个节点代表的是对一个特征的测试，树的分支代表该特征的每一个测试结果，而树的每一个叶子节点代表一个类别。树的最高层是就是根节点\n",
    "\n",
    "二、决策树的学习过程\n",
    "\n",
    "1. 特征选择：特征选择是指从训练数据中众多的特征中选择一个特征作为当前节点的分裂标准，如何选择特征有着很多不同量化评估标准标准，从而衍生出不同的决策树算法。\n",
    "\n",
    "2. 决策树生成： 根据选择的特征评估标准，从上至下递归地生成子节点，直到数据集不可分则停止决策树停止生长。 树结构来说，递归结构是最容易理解的方式。\n",
    "\n",
    "3. 剪枝：决策树容易过拟合，一般来需要剪枝，缩小树结构规模、缓解过拟合。剪枝技术有预剪枝和后剪枝两种。\n",
    "\n",
    "决策树节点的不纯度（impurity） - GINI系数\n",
    "> 节点内尽量是同类的数据\n",
    "\n",
    "$$ GINI= 1- \\sum{c}_{i=1}p(i)^2$$\n",
    "\n",
    "不纯度（impurity）--Entropy熵：\n",
    "$$Entroy = - \\sum_{c}_{i=1}p(i)log P(i)$$\n",
    "\n",
    "\n",
    "三、决策树的三种常用算法\n",
    "* 决策树之ID3算法/基本决策树\n",
    "* 决策树之C4.5算法\n",
    "* 决策树之——CART算法（classification and regression tree）"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 分类问题"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "X = [[0, 0], [1, 1]]\n",
    "Y = [0, 1]\n",
    "clf = DecisionTreeClassifier()\n",
    "clf = clf.fit(X, Y)\n",
    "print(clf.predict([[2., 2.]]))\n",
    "print(clf.predict_proba([[2., 2.]])) # 预测\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 回归问题"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeRegressor\n",
    "X = [[0, 0], [2, 2]]\n",
    "y = [0.5, 2.5]\n",
    "clf = DecisionTreeRegressor()\n",
    "clf = clf.fit(X, y)\n",
    "clf.predict([[1, 1]])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}